# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jaewon

load(
    "@bazel_skylib//lib:collections.bzl",
    "collections",
)
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

def nazel_copts(third_party = False, test = False):
    copts = []
    if not third_party:
        copts += [
            "-Wall",  # -Wall removes -Wno-* so it comes first
            "-Wextra",
            # "-Wswitch-default",
            # "-Wswitch-enum",
            "-Winvalid-pch",
            "-Werror",
            "-ggdb3",
        ]
    copts += [
        "-std=c++17",
        "-fdiagnostics-color",
        "-fsized-deallocation",
        "-finput-charset=UTF-8",
        "-Wno-sign-compare",
        "-Wno-unused-parameter",
        "-Wno-error=uninitialized",
        "-Wno-error=deprecated-declarations",
        "-Wno-error=range-loop-construct",
        "-Wno-error=nonnull",
        "-Wno-maybe-uninitialized",
    ]
    return copts

def nazel_linkopts(test = False):
    return [
        "-pthread",
        "-lstdc++fs",
    ]

def nazel_default_deps():
    return []

def nazel_default_binary_deps():
    return [
    ]

def nazel_cc_library(
        copts = [],
        linkopts = [],
        third_party = False,
        **kwargs):
    cc_library(
        copts = nazel_copts(third_party) + copts,
        linkopts = collections.uniq(nazel_linkopts() + linkopts),
        **kwargs
    )

def nazel_cc_binary(
        copts = [],
        linkopts = [],
        third_party = False,
        deps = [],
        **kwargs):
    cc_binary(
        copts = nazel_copts(third_party) + copts,
        linkopts = collections.uniq(nazel_linkopts() + linkopts),
        deps = collections.uniq(nazel_default_deps() + nazel_default_binary_deps() + deps),
        **kwargs
    )

def nazel_cc_test(
        copts = [],
        linkopts = [],
        link_gtest = True,
        third_party = False,
        deps = [],
        **kwargs):
    if link_gtest:
        deps = deps + [
            "@coin//cc/third_party:gtest",
            "@coin//cc/third_party:gtest_main",
        ]

    # TODO(jaewon): generate coverage result
    cc_test(
        copts = nazel_copts(third_party, True) + copts,
        linkopts = collections.uniq(nazel_linkopts(True) + linkopts),
        deps = collections.uniq(nazel_default_deps() + deps),
        **kwargs
    )

def nazel_cc_pymodule(
        name,
        copts = [],
        linkopts = [],
        deps = [],
        **kwargs):
    if not name.endswith(".so"):
        fail("py_module name must end with `.so`.")

    # TODO(jaewon): linkopts = ["-Wl,-rpath,bazel-bin/_solib_k8/"]
    pybind_extension(
        name = name[:-3],
        copts = nazel_copts() + copts + ["-fvisibility=hidden"],
        linkopts = collections.uniq(nazel_linkopts() + linkopts),
        # deps = collections.uniq(nazel_default_deps() + ["//base:failure_signal_handler"] + deps),
        deps = collections.uniq(nazel_default_deps() + deps),
        #linkshared = True,
        #linkstatic = False,
        **kwargs
    )
